home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: How to use assert( )
- Date: 8 Apr 1996 22:49:16 -0700
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4kctosINNh7p@keats.ugrad.cs.ubc.ca>
- References: <4kc3k7$dur@orion.cybercom.net>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4kc3k7$dur@orion.cybercom.net>,
- John Nield <nield@cybercom.net> wrote:
- >I am interested in the use of the assert() library function (macro?).
- >Neither the FAQ, nor K&R seems to have anything about it. Could
- >someone help me with a breif explanation of its uses and mabye some
- >example code?
-
- K&R2 discusses the <assert.h> facility. Check the index under the A's.
-
- >I'm just starting my first project big enough to split among many
- >people, and from the vague explanations I've heard, assert is supposed
- >to be a usefull way to cause errors when someone passes your code bad
- >values. How do I do this?
-
- #include <assert.h>
-
- .
- .
- .
-
- assert( /* put here an expression that you expect to be true */ );
-
- For example, say you have a function that expects an integer X to be in
- the range [1,1000]. You could do:
-
- assert(X > 0 && X <= 1000); /* lame imitation of range checking */
-
-
-
- To turn the assertions off, put the following statement _before_ your inclusion
- of the <assert.h> header:
-
- #define NDEBUG
- #include <assert.h>
-
- etc.
-
-
- --
-
-